<template>
  <el-menu
    active-text-color="#409EFF"
    background-color="#304156"
    class="el-menu-vertical-demo"
    :default-active="route.path"
    text-color="#fff"
    :collapse="isCollapse"
    :unique-opened="true"
    router
  >
    <el-menu-item index="/index">
      <el-icon><div class="iconfont homepage"></div></el-icon>
      <span>首页</span>
    </el-menu-item>
    <el-sub-menu :index="`/${item.name}`" v-for="item in menus" :key="item.id">
      <template #title>
        <el-icon><div :class="['iconfont', item.icon]"></div></el-icon>
        <span>{{ item.title }}</span>
      </template>
      <template v-for="sub in item.children" :key="sub.id">
        <el-menu-item
          v-if="sub.hidden === 0"
          :index="`/${item.name}/${sub.name}`"
        >
          <el-icon><div :class="['iconfont', sub.icon]"></div></el-icon>
          {{ sub.title }}
        </el-menu-item>
      </template>
    </el-sub-menu>
  </el-menu>
</template>

<script setup lang="ts">
import { inject } from 'vue'
import { useUserStore } from '@/store/user'
import { useRoute } from 'vue-router'

const userStore = useUserStore()
const isCollapse = inject('isCollapse')
const route = useRoute()
const menus = userStore.routerMenu

// 默认打开菜单
// const defMenus = ref('/index')
// defMenus.value = route.path
// watch(
//   () => route.path,
//   () => {
//     defMenus.value = route.path
//   },
//   {
//     immediate: true,
//   },
// )
</script>

<style lang="less" scoped>
.el-menu-vertical-demo {
  height: 100%;
  border: none;
}

.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 250px;
  min-height: 400px;
}
</style>
